package eleven.senven.hpb.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.alibaba.fastjson.JSONObject;
import com.etsy.android.grid.StaggeredGridView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.bitmap.PauseOnScrollListener;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.List;

import eleven.senven.hpb.R;
import eleven.senven.hpb.adapter.OslankaAdapter01;
import eleven.senven.hpb.bean.OslankaBean01;
import eleven.senven.hpb.config.UrlsHolder;
import eleven.senven.hpb.ui.OslankaDtailActivity;

public class RecommendationFragment extends Fragment {

    private StaggeredGridView gridView;
    private List<OslankaBean01.ResultEntity> result;

    public RecommendationFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recommendation, container, false);
        gridView = (StaggeredGridView) view.findViewById(R.id.grid_view);
        loaddata();
        return view;
    }

    private void loaddata() {
        HttpUtils httpUtils = new HttpUtils(10000);
        httpUtils.send(HttpRequest.HttpMethod.GET, UrlsHolder.getInstance().getfirstUrl(1), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                OslankaBean01 oslankaBean01 = JSONObject.parseObject(responseInfo.result, OslankaBean01.class);
                result = oslankaBean01.getResult();
                BitmapUtils bitmapUtils = new BitmapUtils(getActivity());
                gridView.setOnScrollListener( new PauseOnScrollListener(bitmapUtils,true,false));
                gridView.setAdapter(new OslankaAdapter01(result,getActivity(),bitmapUtils));
                gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                        BaseApp.getApplication().show(result.get(position).getFood_name());
                        Intent intent = new Intent(getActivity(), OslankaDtailActivity.class);
                        intent.putExtra("id",result.get(position).getTweet_id());
                        startActivity(intent);
                    }
                });
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }


}
